// import DataBus from '../libs/data.js'; // 导入参数

export default {
	gamedb: {
		gameStatus: 'init', //init初始化  success成功 fail失败
		failTotal: 0, //失败总数
	},
	// 触摸事件处理
	handleTouch(e) {
		const touch = e.touches[0];
		const x = touch.clientX;
		const y = touch.clientY;
		this.game1Click(x, y);
	},
	// 找不同游戏页面点击
	game1Click(x, y) {

		let gameSpots = GameGlobal.databus.gameSpots
		let ctx = GameGlobal.databus.ctx
		let canvas = GameGlobal.databus.canvas
		let btn1Bg = GameGlobal.databus.game1image.btn1Bg//失败按钮1
		let btn2Bg = GameGlobal.databus.game1image.btn2Bg//失败按钮2
		let btn3Bg = GameGlobal.databus.game1image.btn3Bg//成功按钮1
		let btn4Bg = GameGlobal.databus.game1image.btn4Bg//成功按钮2
		//失败按钮1
		const tempbtn1 = {
			x: canvas.width / 2 - btn1Bg.box.w / 2 + btn1Bg.box.x,
			y: canvas.height / 2 - btn1Bg.box.h / 2 + btn1Bg.box.y,
			w: btn1Bg.box.w,
			h: btn1Bg.box.h,
		}
		//失败按钮2
		const tempbtn2 = {
			x: canvas.width / 2 - btn2Bg.box.w / 2 + btn2Bg.box.x,
			y: canvas.height / 2 - btn2Bg.box.h / 2 + btn2Bg.box.y,
			w: btn2Bg.box.w,
			h: btn2Bg.box.h,
		}
		//成功按钮1
		const tempbtn3 = {
			x: canvas.width / 2 - btn3Bg.box.w / 2 + btn3Bg.box.x,
			y: canvas.height / 2 - btn3Bg.box.h / 2 + btn3Bg.box.y,
			w: btn3Bg.box.w,
			h: btn3Bg.box.h,
		}
		//成功按钮2
		const tempbtn4 = {
			x: canvas.width / 2 - btn4Bg.box.w / 2 + btn4Bg.box.x,
			y: canvas.height / 2 - btn4Bg.box.h / 2 + btn4Bg.box.y,
			w: btn4Bg.box.w,
			h: btn4Bg.box.h,
		}

		if (this.gamedb.gameStatus === 'init') {
			// 点击圆圈
			const hasAny = gameSpots.find(spot => {
				return this.isInRect(x, y, spot);
			});
			if (hasAny) {
				this.cikyuan(hasAny)//点击圆圈
			} else {
				this.gamedb.failTotal += 1
				console.log("点击空白", this.gamedb.failTotal)
				if (this.gamedb.failTotal >= 10) {
					this.gamedb.gameStatus = 'fail'
					const Popfail = require('../stage1/common/popfail.js')
					Popfail.default.render(ctx)
				}
			}
		} else if (this.gamedb.gameStatus === 'fail') {
			// 点击失败弹窗按钮
			if (this.isInRect(x, y, tempbtn1)) {
				this.cikfailbtn1()//失败点击退出
			} else if (this.isInRect(x, y, tempbtn2)) {
				this.cikfailbtn2()//失败点击继续
			}
		} else if (this.gamedb.gameStatus === 'success') {
			// 点击重来弹窗按钮
			if (this.isInRect(x, y, tempbtn3)) {
				this.ciksuccessbtn1()//成功点击重来

			} else if (this.isInRect(x, y, tempbtn4)) {
				this.ciksuccessbtn2()//成功点击返回
			}
		}
	},

	// 点击圆圈
	cikyuan(hasAny) {
		let ctx = GameGlobal.databus.ctx
		let gameSpots = GameGlobal.databus.gameSpots
		hasAny.isClicked = true;
		// 画水墨圆
		this.cavasyuan((hasAny.x + hasAny.w / 2), (hasAny.y + hasAny.h / 2), (hasAny.h / 2 + 10), () => {
			const totalClicked = gameSpots.filter(item => item.isClicked === true).length;
			if (totalClicked >= 5) {
				this.gamedb.gameStatus = 'success'
				const Popok = require('../stage1/common/popok.js')
				Popok.default.render(ctx)
			}
		})
	},
	// 失败点击退出
	cikfailbtn1() {
		this.gamedb.failTotal = 0
		this.gamedb.gameStatus = 'init'
		GameGlobal.databus.gameSpots = []
		GameGlobal.databus.gameState = 'home'
		GameGlobal.databus.homethis.render()
		console.log("点击退出游戏")
	},
	// 失败点击继续
	cikfailbtn2() {
		const that = this
		GameGlobal.databus.videoAd.show().then(() => { })
		function listener(res) {
			GameGlobal.databus.videoAd.offClose(listener);
			
			console.log('取消观看视频',res.isEnded)
			if (res.isEnded) {

				that.gamedb.failTotal = 0
				that.gamedb.gameStatus = 'init'
				// gameSpots.forEach(spot => spot.isClicked = false);
				GameGlobal.databus.gamethis.update()
			} else {
			}
			if (res.count) {
				console.log('观看视频数量：', res.count)
			}
		}
		GameGlobal.databus.videoAd.onClose(listener); //这里监听广告的close 事件
	},
	// 成功点击重来
	ciksuccessbtn1() {
		let gameSpots = GameGlobal.databus.gameSpots
		this.gamedb.failTotal = 0
		this.gamedb.gameStatus = 'init'
		gameSpots.forEach(spot => spot.isClicked = false);
		GameGlobal.databus.gamethis.update()
		console.log("点击观看视频继续")
	},
	// 成功点击返回
	ciksuccessbtn2() {
		console.log("点击返回");
		this.gamedb.failTotal = 0
		this.gamedb.gameStatus = 'init'
		GameGlobal.databus.gameSpots = []
		GameGlobal.databus.gameState = 'home'
		GameGlobal.databus.homethis.render()
	},

	// 水墨圆
	cavasyuan(x, y, r, func) {
		let ctx = GameGlobal.databus.ctx
		let progress = 0;
		const center = { x: x, y: y };
		const radius = r;

		function draw() {
			// 清空画布
			// ctx.clearRect(0, 0, canvas.width, canvas.height);
			// 创建水墨渐变
			const gradient = ctx.createRadialGradient(
				center.x, center.y, radius * 0.6,
				center.x, center.y, radius
			);
			gradient.addColorStop(0, 'rgba(200,0,0,0.9)');
			gradient.addColorStop(1, 'rgba(200,0,0,0.2)');
			// 绘制动画笔触
			ctx.beginPath();
			ctx.arc(center.x, center.y, radius, -Math.PI / 2, -Math.PI / 2 + progress * Math.PI * 2);
			ctx.lineWidth = 3;
			ctx.strokeStyle = 'rgba(200,0,0,0.9)';
			ctx.lineCap = 'round';

			// 添加毛边效果
			ctx.shadowColor = 'rgba(200,0,0,0.5)';
			ctx.shadowBlur = 3;

			ctx.stroke();

			// 更新进度（缓动动画）
			progress += (1 - progress) * 0.04;
			if (progress < 0.90) {
				if (GameGlobal.databus.gameState === 'game1') {
					requestAnimationFrame(draw);
				}
			} else {
				func && func()
			}
		}
		requestAnimationFrame(draw);
	},
	// 辅助函数
	isInRect(x, y, rect) {
		return x >= rect.x && x <= rect.x + rect.w &&
			y >= rect.y && y <= rect.y + rect.h;
	},

	isInCircle(x, y, circle) {
		const dx = x - circle.x;
		const dy = y - circle.y;
		return dx * dx + dy * dy <= circle.r * circle.r;
	}
}